home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / SetDeskCPatDemo / SetDeskCPat.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  3.4 KB  |  187 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SetDeskCPat.c
  3.  
  4.     Contains:    Simple code sample which demonstrates how to call SetDeskCPat properly.  
  5.  
  6.     Written by: Pete Gontier    
  7.  
  8.     Copyright:    Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/14/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #define OLDROUTINELOCATIONS            0
  25. #define OLDROUTINENAMES                0
  26. #define SystemSevenOrLater            1
  27.  
  28. #include <Sound.h>
  29.  
  30. #ifndef __FONTS__
  31. #    include <Fonts.h>
  32. #endif
  33.  
  34. #ifndef __ERRORS__
  35. #    include <Errors.h>
  36. #endif
  37.  
  38. #ifndef __RESOURCES__
  39. #    include <Resources.h>
  40. #endif
  41.  
  42. #include "MoveableModalDialog.h"
  43.  
  44. enum
  45. {
  46.     kDialogItem_Button_None,
  47.     kDialogItem_Button_Quit,
  48.     kDialogItem_Button_RGB,
  49.     kDialogItem_Button_Resource,
  50.     kDialogItem_Button_NIL
  51. };
  52.  
  53. static pascal OSErr InitMac (void)
  54. {
  55.     MaxApplZone ( );
  56.     InitGraf (&(qd.thePort));
  57.     InitFonts ( );
  58.     InitWindows ( );
  59.     InitMenus ( );
  60.     TEInit ( );
  61.     InitDialogs (nil);
  62.  
  63.     return noErr;
  64. }
  65.  
  66. static pascal OSErr myMakeRGBPat
  67.     (const RGBColor *color, Boolean inSysHeap, PixPatHandle *pphp)
  68. {
  69.     OSErr err = noErr;
  70.  
  71.     THz savedZone;
  72.  
  73.     if (inSysHeap)
  74.     {
  75.         savedZone = GetZone ( );
  76.         SetZone (SystemZone ( ));
  77.     }
  78.  
  79.     if (!(*pphp = NewPixPat ( )))
  80.         err = QDError ( );
  81.     else
  82.     {
  83.         MakeRGBPat (*pphp,color);
  84.         err = QDError ( );
  85.         if (err)
  86.         {
  87.             DisposePixPat (*pphp);
  88.             *pphp = nil;
  89.         }
  90.     }
  91.  
  92.     if (inSysHeap)
  93.         SetZone (savedZone);
  94.  
  95.     return err;
  96. }
  97.  
  98. static pascal OSErr myGetPixPat (short resID, Boolean inSysHeap, PixPatHandle *pphp)
  99. {
  100.     OSErr err = noErr;
  101.  
  102.     Handle pixPatResH = GetResource ('ppat',resID);
  103.     if (!(err = ResError ( )))
  104.     {
  105.         
  106.         if (!pixPatResH)
  107.             err = resNotFound;
  108.         else
  109.         {
  110.             THz savedZone;
  111.  
  112.             if (inSysHeap)
  113.             {
  114.                 savedZone = GetZone ( );
  115.                 SetZone (SystemZone ( ));
  116.             }
  117.  
  118.             *pphp = GetPixPat (resID);
  119.             if (!*pphp) err = QDError ( );
  120.  
  121.             if (inSysHeap)
  122.                 SetZone (savedZone);
  123.         }
  124.     }
  125.  
  126.     return err;
  127. }
  128.  
  129. static pascal OSErr SetDeskCPatFromRGB (void)
  130. {
  131.     PixPatHandle pph = nil;
  132.     RGBColor rgbGray = { 0x7FFF, 0x7FFF, 0x7FFF };
  133.     OSErr err = myMakeRGBPat (&rgbGray,true,&pph);
  134.     if (!err) SetDeskCPat (pph);
  135.     return err;
  136. }
  137.  
  138. static pascal OSErr SetDeskCPatFromResource (void)
  139. {
  140.     PixPatHandle pph = nil;
  141.     OSErr err = myGetPixPat (128,true,&pph);
  142.     if (!err) SetDeskCPat (pph);
  143.     return err;
  144. }
  145.  
  146. void main (void)
  147. {
  148.     if (InitMac ( ))
  149.         SysBeep (10);
  150.     else
  151.     {
  152.         DialogRef dlgRef = GetNewDialog (128,nil,(WindowRef)-1);
  153.         if (dlgRef)
  154.         {
  155.             short itemHit;
  156.  
  157.             SetDialogDefaultItem (dlgRef,kDialogItem_Button_Quit);
  158.  
  159.             do
  160.             {
  161.                 MoveableModalDialog (NewModalFilterProc(StdFilterProc),&itemHit);
  162.  
  163.                 switch (itemHit)
  164.                 {
  165.                     case kDialogItem_Button_RGB            :
  166.  
  167.                         (void) SetDeskCPatFromRGB ( );
  168.                         break;
  169.  
  170.                     case kDialogItem_Button_Resource    :
  171.  
  172.                         (void) SetDeskCPatFromResource ( );
  173.                         break;
  174.  
  175.                     case kDialogItem_Button_NIL            :
  176.  
  177.                         SetDeskCPat (nil);
  178.                         break;
  179.                 }
  180.             }
  181.             while (itemHit != kDialogItem_Button_Quit);
  182.  
  183.             DisposeDialog (dlgRef);
  184.         }
  185.     }
  186. }
  187.